package com.meteor.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.meteor.pojo.Type;
import com.meteor.service.TypeService;
import com.meteor.util.BlogBean;
import com.meteor.pojo.Tag;
import com.meteor.service.BlogService;
import com.meteor.service.TagService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Controller
public class IndexController {

    @Resource
    private BlogService blogService;

    @Resource
    private TagService tagService;

    @Resource
    private TypeService typeService;

    @GetMapping("/")
    public String indexPro() {
        return "redirect:/1";
    }

    /**
     * 博客首页面访问
     */
    @GetMapping("/{pageNum}")
    public String index(@PathVariable Integer pageNum, Model model) {
        PageHelper.startPage(pageNum, 5);
        List<BlogBean> blogs = blogService.queryBlog();
        PageInfo<BlogBean> pageInfo = new PageInfo<>(blogs);
        List<Type> types = typeService.queryTypes();
        model.addAttribute("count",pageInfo.getTotal());
        Collections.sort(blogs);
        List<Type> types1 = types.subList(0, pageInfo.getSize());
        System.out.println(pageInfo.getSize());
        model.addAttribute("types", types1);
        model.addAttribute("blogs", blogs);
        model.addAttribute("page", pageInfo);
        return "index";
    }

    /**
     * 博客详情页面访问
     */
    @GetMapping("/blog")
    public String blog() {
        return "blog";
    }

    /**
     * 根据标题进行全局搜索
     */
    @RequestMapping("/selectBlogByTitle")
    @ResponseBody
    public List<BlogBean> selectBlogByTitle(String title) {
        List<BlogBean> blogBeans = blogService.queryBlogByTitle(title);
        if(blogBeans != null) {
            return blogBeans;
        }else {
            return null;
        }
    }

}

















